﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using EnvDTE;
using System.IO;
using SPVisualDev.Services;

namespace SPVisualDev.VSAddin
{
    public class PushDownManager
    {
        private SpecialProject m_project;
        private List<string> m_exclude_path_list;

        public PushDownManager(SpecialProject project)
        {
            m_project = project;
        }

        public IMainService RemoteService
        {
            get;
            set;
        }

        public IList<string> ExcludedPaths
        {
            get { return m_exclude_path_list; }
        }

        private void Init()
        {
            m_exclude_path_list = new List<string>();
        }

        public bool IsPathExcluded(string Path)
        {
            string ret = m_exclude_path_list.Find(s => Path.StartsWith(s, StringComparison.OrdinalIgnoreCase));
            bool b = (ret != null);
            return b;
        }

        public void AddPath(string Path, bool IgnoreIfExists)
        {
            string s = Path.ToLower();

            List<string> deleteList = m_exclude_path_list.FindAll(delegate(string CurrentPath)
            {
                return CurrentPath.ToLower() == s || CurrentPath.ToLower().StartsWith(s + "\\");
            });

            foreach (string path in deleteList)
            {
                m_exclude_path_list.Remove(path);
            }

            m_exclude_path_list.Add(s);

            /*
            if (m_exclude_path_list.Contains(s))
            {
                if (!IgnoreIfExists)
                {
                    throw new Exception("Path already exists");
                }
            }
            else
            {
                m_exclude_path_list.Add(s);
            }*/
        }

        public void DeletePath(string Path, bool Recursive)
        {
            string s = Path.ToLower();
            List<string> deleteList = m_exclude_path_list.FindAll(delegate(string CurrentPath)
            {
                return CurrentPath.ToLower() == s || (Recursive && CurrentPath.ToLower().StartsWith(s + "\\"));
            });

            foreach (string path in deleteList)
            {
                m_exclude_path_list.Remove(path);
            }
        }

        public void ReadSettings()
        {
            if (m_project == null)
                return;

            Init();

            string exclusion_string = m_project.GetUserSettingsValue("PushDownExclusionList");
            if (string.IsNullOrEmpty(exclusion_string))
            {
                m_exclude_path_list = new List<string>();
                return;
            }

            string[] exclusion_list = exclusion_string.Split(';');
            m_exclude_path_list = new List<string>(exclusion_list);
        }

        public void WriteSettings()
        {
            StringBuilder builder = new StringBuilder();

            foreach (string s in m_exclude_path_list)
            {
                if (builder.Length > 0)
                {
                    builder.Append(";" + s);
                }
                else
                {
                    builder.Append(s);
                }
            }

            m_project.SetUserSettingsValue("PushDownExclusionList", builder.ToString(), true);
        }
    }
}
